
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.dto.DictUserRoleDto;
import com.rt.schedulebase.entity.DictUserRole;
import com.rt.schedulebase.mapper.DictUserRoleMapper;
import com.rt.schedulenew.api.IDictUserRoleService;
import com.rt.schedulenew.utils.base.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictUserRoleServiceImpl extends ServiceImpl<DictUserRoleMapper, DictUserRole> implements IDictUserRoleService {
    @Override
    public boolean insertSelective(DictUserRole dictUserRole) {
        return this.retBool(baseMapper.insert(dictUserRole));
    }

    @Override
    public boolean deleteByPrimaryKey(String key) {
        return this.retBool(baseMapper.deleteById(key));
    }

    @Override
    public boolean updateByPrimaryKeySelective(DictUserRole dictUserRole) {
        return this.retBool(baseMapper.updateById(dictUserRole));
    }

    @Override
    public DictUserRole selectByPrimaryKey(String key) {
        return baseMapper.selectById(key);
    }

    @Override
    public List<DictUserRole> getList(DictUserRoleDto dto) {
        QueryWrapper<DictUserRole> qw = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("USER_ID", dto.getUserId());
        map.put("ROLE_ID", dto.getRoleId());
        qw.allEq(map, false);
        List<DictUserRole> list = (baseMapper).selectList(qw);
        return list;
    }

    @Override
    public List<DictUserRole> getListAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean delete(DictUserRole dictUserRole) {
        UpdateWrapper<DictUserRole> uw = new UpdateWrapper<>();
        if (StringUtils.isNotBlank(dictUserRole.getUserId())) {
            uw.in("USER_ID", CommonUtil.split2List(dictUserRole.getUserId()));
        }
        if (StringUtils.isNotBlank(dictUserRole.getRoleId())) {
            uw.in("ROLE_ID", CommonUtil.split2List(dictUserRole.getRoleId()));
        }
        return this.retBool(baseMapper.delete(uw));
    }

    @Override
    public boolean update(DictUserRole dictUserRole) {
        UpdateWrapper<DictUserRole> uw = new UpdateWrapper<>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("USER_ID", dictUserRole.getUserId());
        map.put("ROLE_ID", dictUserRole.getRoleId());
        uw.allEq(map, false);
        return this.retBool(baseMapper.update(dictUserRole, uw));
    }

    @Override
    public DictUserRole getInfo(DictUserRoleDto dto) {
        QueryWrapper<DictUserRole> qw = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("USER_ID", dto.getUserId());
        map.put("ROLE_ID", dto.getRoleId());
        qw.allEq(map, false);
        return baseMapper.selectOne(qw);
    }

    @Override
    public boolean insertUserBatch(DictUserRole dictUserRole) {
        List<String> userIds = CommonUtil.split2List(dictUserRole.getUserId());
        for (String userId : userIds) {
            dictUserRole.setUserId(userId);
            baseMapper.insert(dictUserRole);
        }
        return true;
    }

    @Override
    public boolean insertRoleBatch(DictUserRole dictUserRole) {
        List<String> roleIds = CommonUtil.split2List(dictUserRole.getRoleId());
        for (String roleId : roleIds) {
            dictUserRole.setRoleId(roleId);
            baseMapper.insert(dictUserRole);
        }
        return true;
    }

    @Override
    public List<DictUserRoleDto> getUserRoleList(DictUserRoleDto dto) {
        return baseMapper.getUserRoleList(dto);
    }

    @Override
    public List<DictUserRoleDto> getRoleUserList(DictUserRoleDto dto) {
        return baseMapper.getRoleUserList(dto);
    }

    @Override
    public List<DictUserRoleDto> getUserRoleListForInsert(DictUserRoleDto dto) {
        return baseMapper.getUserRoleListForInsert(dto);
    }

    @Override
    public List<DictUserRoleDto> getRoleUserListForInsert(DictUserRoleDto dto) {
        if (StringUtils.isNotBlank(dto.getUserId())) {
            dto.setUserId(dto.getUserId().toUpperCase());
        }
        return baseMapper.getRoleUserListForInsert(dto);
    }
}
